﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ue.Framework.Web.WebControls;
using Ue.Infrastructure.Web.UI;

namespace Ue.Qcdz.UI.Controls.Feedbacks
{
    public class CommentsEdit : SkinnedEntityEdit<Ue.Qcdz.Domain.feedbacks.Feedback>
    {
        private string skinFileName = "~/Themes/zh-cn/Skins/FeedBacks/FeedBackEdits.ascx";
        private TextBox tbxName;
        private TextBox tbxContact;
        private TextBox tbxTitle;
        private TextBox tbxComment;
        public bool IsComment { get; set; }
        #region Overrides of SkinnedControl

        protected override string GetSkinFileName()
        {
            return skinFileName;
        }

        #endregion

        #region Overrides of SkinnedEntityEdit<Feedback>

        protected override void InitSkin(Control skin)
        {


            if (string.IsNullOrEmpty(this.Page.Request["shopId"]) && !IsComment)
            {
                MessageWindow.RenderMessage("参数错误", "/default.aspx");
            }
            else
            {
                tbxName = skin.FindControl<TextBox>("tbxName");
                tbxContact = skin.FindControl<TextBox>("tbxContact");
                tbxTitle = skin.FindControl<TextBox>("tbxTitle");
                tbxComment = skin.FindControl<TextBox>("tbxComment");
            }


        }
        private string GetRealIP()
        {
            string ip;
            try
            {
                HttpRequest request = HttpContext.Current.Request;

                if (request.ServerVariables["HTTP_VIA"] != null)
                {
                    ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString().Split(',')[0].Trim();
                }
                else
                {
                    ip = request.UserHostAddress;
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            return ip;
        }
        protected override Ue.Qcdz.Domain.feedbacks.Feedback Enttiy
        {
            get
            {
                if (this.Action == "Delete")
                {
                    return new Ue.Qcdz.Domain.feedbacks.Feedback((Guid)this.EntityKey);
                }

                string shopId = null;
                string currentIp = GetRealIP();
                shopId = IsComment ? "comm" : this.Page.Request["shopId"];


                return new Ue.Qcdz.Domain.feedbacks.Feedback(
                    tbxName.GetValue<string>(),
                    tbxContact.GetValue<string>(),
                    tbxTitle.GetValue<string>(),
                    tbxComment.GetValue<string>(),
                    shopId,
                    currentIp,
                    this.EntityKey
                    );
            }
            set
            {
                if (value == null)
                    return;
                tbxName.SetValue(value.Name);
                tbxComment.SetValue(value.Comment);
                tbxContact.SetValue(value.Contact);
                tbxTitle.SetValue(value.Title);
            }
        }

        #endregion
    }
}
